package com.ruoyi.web.controller.sample.Sender;

import com.ruoyi.samplemod.domain.entity.Sample;
import com.ruoyi.samplemod.mapper.EmailMapper;
import com.ruoyi.samplemod.service.ACBSampleService;
import com.ruoyi.samplemod.service.ACSampleService;
import com.ruoyi.samplemod.service.EmailService;
import com.ruoyi.samplemod.service.SampleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Component
public class SchenduledEmailSender {

    @Autowired
    private EmailService emailService;

    @Autowired
    private SampleService sampleService;

    @Autowired
    private ACSampleService acsampleService;

    @Autowired
    private ACBSampleService acbsampleService;

    private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    private Date entTime = new Date();
    private Date lastTime = new Date();

    @Autowired
    private EmailMapper emailMapper;


//    @Scheduled(cron = "*/30 * * * * ?")
    @Scheduled(cron = "0 0 9 * * ?")
    public void sendEmail() throws ParseException {
        List<String> emailList = emailMapper.getEmailByMenuId(2000L);
        List<Sample> samples = sampleService.selectSampleAll();
        StringBuffer mail=new StringBuffer();
        for (Sample sample : samples) {
            getSampleM(mail, sample);
        }
        if (!mail.toString().isEmpty()){
            for (String email : emailList) {
                emailService.sendSimpleMail(email,"自动发送临期/过期样本",mail.toString());
            }
        }
        forTheEmail(acsampleService.selectSampleAllOrderByDept());
        forTheEmail(acbsampleService.selectSampleAllOrderByDept());


    }

    private void forTheEmail(List<Sample> acbSamples) throws ParseException {
        Map<Long, List<Sample>> deptMapSample = new HashMap<>();
        for (Sample acSample : acbSamples) {
            Long deptId = acSample.getSysDeptId();
            deptMapSample.computeIfAbsent(deptId, k -> new ArrayList<>()).add(acSample);
        }

        for (Long key : deptMapSample.keySet()) {
            StringBuffer acbmail=new StringBuffer();
            for (Sample sample : deptMapSample.get(key)) {
                getSampleM(acbmail, sample);
            }
            List<String> acbemailList = emailMapper.getEmailByMenuIdByDeptId(2032L,key);
            if (!acbmail.toString().isEmpty()){
                for (String email : acbemailList) {
                    emailService.sendSimpleMail(email,"自动发送临期/过期样本",acbmail.toString());
                }
            }
        }
    }

    private void getSampleM(StringBuffer acbmail, Sample sample) throws ParseException {
        lastTime = dateFormat.parse(sample.getLastTime());
        if ((((lastTime.getTime()-entTime.getTime())/(24*60*60*1000))<30) && (lastTime.getTime()-entTime.getTime())>0) {
            acbmail.append("样本名称: "+sample.getSampleName()+"   样本编号: "+sample.getSampleCode()+"   到期时间: "+sample.getLastTime()+"    临期"+"\n");
        }
        if (lastTime.getTime()-entTime.getTime()<0){
            acbmail.append("样本名称: "+sample.getSampleName()+"   样本编号:"+sample.getSampleCode()+"    到期时间: "+sample.getLastTime()+"    过期"+"\n");
        }
    }

}
